package org.example.c2;

import lombok.extern.slf4j.Slf4j;

/**
 * @author liu-rui
 * @date 2020/6/3 下午5:36
 * @description
 * @since
 */
@Slf4j
public class SumTask {
    public void exec(ICounter counter) throws InterruptedException {
        Thread[] threads = new Thread[10];

        for (int i = 0; i < threads.length; i++) {
            final Thread thread = new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    counter.add(1);
                }
            });
            threads[i] = thread;
            thread.start();
        }

        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        log.info("{}", counter.getSum());
    }
}
